<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicModal
    v-bind="$attrs"
    :title="t('材料信息')"
    :okText="t('确认')"
    @register="registerModal"
    @ok="handleSubmit"
    width="40%"
    :minHeight="320"
  >
  <!--     :minHeight="120"
    :width="800" -->
    <!-- 多行文本框 -->
     <a-textarea
      v-model:value="uploadInfo"
      placeholder="材料编码	材料名称	规格型号	单位	数量"
      style="width: 100%"
      :auto-size="{ minRows: 14, maxRows: 30 }"
    />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bas.cus.basCusKpinfo');

  const uploadInfo = ref('');
  let materialFlag = ref<any>('');

  const [registerModal, { setModalProps, closeModal }] = useModalInner((data:any) => {
    materialFlag.value = data.materialFlag;
  });


  async function handleSubmit() {
    // 把得到的值传递给父组件
    // 把excel 粘贴过来的值转换成数组对象格式，每一行为一组，每一组的每个单元格为对象
    let arr = uploadInfo.value.split('\n');
    
    for (var i in arr) {
      // 去除arr[i]中value 为空值的对象，去除arr[i]中value 为空值的对象
      arr[i] = arr[i].split('\t');
      // 如果arr[i]中value 为空值，则跳出本次循环
      if (arr[i] == '') {
        break;
      }
      // 去除arr[i]中value 为空值的对象
      if(materialFlag.value == 1){
        // 头部追加
        arr[i].unshift(1);
      }else if(materialFlag.value == 2){
        // 尾部追加
        arr[i].unshift(2);
      }
    }
    // 去除数组arr 中value 为空值的对象
    // arr = arr.filter(item => item !== '\t' && item !== '');
    // 把arr数组变成对象数组
    
    for (var i in arr) {
      // 如果arr[i][1] ,arr[i][2],arr[i][3],arr[i][5] 都不能为空或者 undefined
      arr[i] = {
        ctype: arr[i][0],
        mcode: arr[i][1],
        mname: arr[i][2],
        mstd: arr[i][3],
        unit: arr[i][4],
        num: arr[i][5],
      };
    }
    console.log(arr,'arr===');
    // 传递给父组件
    emit('success', arr);
    closeModal();

  }
</script>
